package com.wei.czz.framework.rabbitMQ.listener;

import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.rabbitMQ.BlogCommentMessageEnum;
import com.wei.czz.common.enums.rabbitMQ.MqEnum;
import com.wei.czz.framework.blog.service.BlogCommentService;
import com.wei.czz.framework.rabbitMQ.message.blog.BlogCommentMessage;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2022-03-04 17:14:58
 * className: BlogCommentQueueListener 博客评论消息队列监听类
 * version: 1.0
 * description:
 */
@Component
@AllArgsConstructor
public class BlogCommentQueueListener {

    private static final Logger log = LoggerFactory.getLogger(BlogCommentQueueListener.class);

    private final BlogCommentService blogCommentService;

    /**
     * 系统博客评论点赞数变化消息监听方法
     * 根据博客评论消息，增加或者减少博客评论的点赞数量
     * @param blogCommentMessage 博客评论消息对象
     */
    @RabbitListener(queues = { MqEnum.Queue.BLOG_COMMENT_LIKE })
    public void blogCommentLikeMessage(BlogCommentMessage blogCommentMessage) {
        BlogCommentMessageEnum blogCommentMessageEnum = BlogCommentMessageEnum.getType(blogCommentMessage.getType());
        if (blogCommentMessageEnum == null) {
            log.error("博客评论消息未知，请务必及时处理");
            return;
        }
        log.info("开始异步更新博客评论{}业务。{}", blogCommentMessageEnum.getName(), blogCommentMessage);

        Date date = new Date();

        if (blogCommentMessageEnum == BlogCommentMessageEnum.LIKE_TYPE) {
            /*
                博客评论点赞数变化
             */
            blogCommentService.updateBlogCommentLikeNum(blogCommentMessage.getCommentId(), blogCommentMessage.getNum(),
                    date, CommonEnum.ZERO.getLongValue());
        }

        log.info("异步更新博客评论{}业务结束", blogCommentMessageEnum.getName());
    }
}
